﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2018/5/16 15:16:59------------------------------------------
----------------------------------------------------------------------------------*/


using Donal.Xms.Common;
using Donal.Xms.DBUtility;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Donal.Xms.DAL
{
    /// <summary>
    /// 数据访问类
    /// </summary>
    public partial class order_goods
    {
        private string databaseprefix; //数据库表名前缀
        public order_goods(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from  " + databaseprefix + "order_goods");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                        new SqlParameter("@id", SqlDbType.Int,10)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.order_goods model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1 = new StringBuilder();//数据字段
            StringBuilder str2 = new StringBuilder();//数据参数
                                                     //利用反射获得属性的所有公共属性
            PropertyInfo[] pros = model.GetType().GetProperties();
            List<SqlParameter> paras = new List<SqlParameter>();
            strSql.Append("insert into " + databaseprefix + "order_goods(");
            foreach (PropertyInfo pi in pros)
            {
                //如果不是主键则追加sql字符串
                if (!pi.Name.Equals("id"))
                {
                    //判断属性值是否为空
                    if (pi.GetValue(model, null) != null)
                    {
                        str1.Append(pi.Name + ",");//拼接字段
                        str2.Append("@" + pi.Name + ",");//声明参数
                        paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null)));//对参数赋值
                    }
                }
            }
            strSql.Append(str1.ToString().Trim(','));
            strSql.Append(") values (");
            strSql.Append(str2.ToString().Trim(','));
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY;");
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), paras.ToArray());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.order_goods model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1 = new StringBuilder();
            //利用反射获得属性的所有公共属性
            PropertyInfo[] pros = model.GetType().GetProperties();
            List<SqlParameter> paras = new List<SqlParameter>();
            strSql.Append("update  " + databaseprefix + "order_goods set ");
            foreach (PropertyInfo pi in pros)
            {
                //如果不是主键则追加sql字符串
                if (!pi.Name.Equals("id"))
                {
                    //判断属性值是否为空
                    if (pi.GetValue(model, null) != null)
                    {
                        str1.Append(pi.Name + "=@" + pi.Name + ",");//声明参数
                        paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null)));//对参数赋值
                    }
                }
            }
            strSql.Append(str1.ToString().Trim(','));
            strSql.Append(" where id=@id ");
            paras.Add(new SqlParameter("@id", model.id));
            return DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id, Model.manager manager, Model.userconfig userconfig)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        Model.order_goods order_Goods = GetModel(conn, trans, id);

                        if (order_Goods == null)
                        {
                            trans.Rollback();
                            return false;
                        }

                        DAL.orders ordersDal = new DAL.orders(databaseprefix);

                        Model.orders orders = ordersDal.GetModel(conn, trans, order_Goods.order_id);

                        if (orders == null)
                        {
                            trans.Rollback();
                            return false;
                        }

                        company _dal_company = new DAL.company(databaseprefix);
                        Model.company company = _dal_company.GetModel(conn, trans, "");
                        if (company == null)
                        {
                            trans.Rollback();
                            return false;
                        }
                                                

                        orders.status = 1;                                                 

                        if (!ordersDal.Update(conn, trans, orders))//更新订单
                        {
                            trans.Rollback();
                            return false;
                        }

                        if (!Delete(conn, trans, id))//删除商品
                        {
                            trans.Rollback();
                            return false;
                        }

                        string money_remark = "待财务退款";
                        int _status = 0;
                        

                        if (new DAL.logs(databaseprefix).Add(conn, trans, new Model.logs()
                        {
                            action_id = orders.id,
                            log_type = 1,
                            operation_content = "删除商品：" + order_Goods.goods_title + "(" + order_Goods.erp_goods_no + ") ！",
                            operation_time = DateTime.Now,
                            operation_type = "删除商品",
                            user_name = manager.user_name,
                            user_type = manager.real_name
                        }) == 0)
                        {
                            trans.Rollback();
                            return false;
                        };                                               

                        //解除锁定
                        DbHelperSQL.ExecuteSql(conn, trans, "update dt_points_logs set status = 0 where status = " + orders.id);

                        trans.Commit();
                        conn.Close();
                        return true;
                    }
                    catch
                    {
                        trans.Rollback();
                        conn.Close();
                        return false;
                    }
                }
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from  " + databaseprefix + "order_goods ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                        new SqlParameter("@id", SqlDbType.Int,10)};
            parameters[0].Value = id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(SqlConnection conn, SqlTransaction trans, int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from  " + databaseprefix + "order_goods ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                        new SqlParameter("@id", SqlDbType.Int,10)};
            parameters[0].Value = id;

            return DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.order_goods GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1 = new StringBuilder();
            Model.order_goods model = new Model.order_goods();
            //利用反射获得属性的所有公共属性
            PropertyInfo[] pros = model.GetType().GetProperties();
            foreach (PropertyInfo p in pros)
            {
                str1.Append(p.Name + ",");//拼接字段
            }
            strSql.Append("select top 1 " + str1.ToString().Trim(','));
            strSql.Append(" from " + databaseprefix + "order_goods");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                        new SqlParameter("@id", SqlDbType.Int,10)};
            parameters[0].Value = id;
            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];

            if (dt.Rows.Count > 0)
            {
                return DataRowToModel(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.order_goods GetModel(SqlConnection conn, SqlTransaction trans, int id)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1 = new StringBuilder();
            Model.order_goods model = new Model.order_goods();
            //利用反射获得属性的所有公共属性
            PropertyInfo[] pros = model.GetType().GetProperties();
            foreach (PropertyInfo p in pros)
            {
                str1.Append(p.Name + ",");//拼接字段
            }
            strSql.Append("select top 1 " + str1.ToString().Trim(','));
            strSql.Append(" from " + databaseprefix + "order_goods");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                        new SqlParameter("@id", SqlDbType.Int,10)};
            parameters[0].Value = id;
            DataTable dt = DbHelperSQL.Query(conn, trans, strSql.ToString(), parameters).Tables[0];

            if (dt.Rows.Count > 0)
            {
                return DataRowToModel(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetListView(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" og.invoice_id,og.goods_money,og.quantity,ogl.* FROM dt_order_goods og ");
            strSql.Append(" inner join dt_order_goods_logs ogl on og.order_id = ogl.order_id and og.goods_id = ogl.goods_id and og.cart_type = ogl.cart_type and og.is_tehui = ogl.is_tehui ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM " + databaseprefix + "order_goods");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetPrintList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM view_goods_print_list ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM " + databaseprefix + "order_goods");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetGoodsOrderExpressList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM view_goods_ordrer_express");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 返回指定列
        /// </summary>
        public string GetValue(int id, string fileName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 " + fileName + " from " + databaseprefix + "order_goods");
            strSql.Append(" where id=" + id);
            string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(title))
            {
                return "";
            }
            return title;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + databaseprefix + "order_goods set " + strValue);
            strSql.Append(" where id=" + id);
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 将对象转换实体
        /// </summary>
        public Model.order_goods DataRowToModel(DataRow row)
        {
            Model.order_goods model = new Model.order_goods();
            if (row != null)
            {
                //利用反射获得属性的所有公共属性
                Type modelType = model.GetType();
                for (int i = 0; i < row.Table.Columns.Count; i++)
                {
                    //查找实体是否存在列表相同的公共属性
                    PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName);
                    if (proInfo != null && row[i] != DBNull.Value)
                    {
                        proInfo.SetValue(model, row[i], null);//用索引值设置属性值
                    }
                }
            }
            return model;
        }


        /// <summary>
        /// 将选中的产品分成一个订单
        /// </summary>
        public bool SplitGoodsToOrder(List<Model.order_product_split> ops)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        int target_order_id = 0;//目标订单ID
                        List<int> wait_del_ordre_ids = new List<int>();//待删除订单ID
                        List<int> wait_split_goods_ids = new List<int>();//待分配产品ID
                        List<int> other_goods_ids = new List<int>();//其它产品ID

                        int _all_goods_num = 0;

                        foreach (Model.order_product_split v in ops)
                        {
                            _all_goods_num += v.order_goods_id.Count;

                            foreach (int s in v.order_goods_id)
                            {
                                wait_split_goods_ids.Add(s);
                            }

                            foreach (int s in v.other_goods_id)
                            {
                                other_goods_ids.Add(s);
                            }

                            if (v.goods_num == v.order_goods_id.Count)
                            {
                                if (target_order_id == 0)
                                {
                                    target_order_id = v.order_id;//第一个子订单下的所有产品都被选中，将这个订单作为其它子订单下产品移入的对象订单（即目标订单）。
                                }
                                else
                                {
                                    wait_del_ordre_ids.Add(v.order_id);//如果还有其它的子订单下的所有产品都被选中，那么将这个订单加入待删除列表（即待删除订单）。
                                }
                            }
                        }

                        if (target_order_id == 0)//没有目标订单
                        {
                            if (ops.Count == 1 && ops[0].parent_id == 0)//只有一个订单要分成两个子订单
                            {
                                //只有一个父订单要分成两个子订单
                                //创建两个新订单，将所有待分配产品ID移动到第一个子订单中，其余的移动到第二个子订单。

                                if (!SplitOrder(conn, trans, ops, 1, 0, null, wait_split_goods_ids))
                                {
                                    trans.Rollback();
                                    conn.Close();
                                    return false;
                                }
                            }
                            else
                            {
                                //创建一个新订单，将所有待分配产品ID都移动到新订单下面。

                                if (!SplitOrder(conn, trans, ops, 2, 0, null, wait_split_goods_ids))
                                {
                                    trans.Rollback();
                                    conn.Close();
                                    return false;
                                }
                            }
                        }
                        else//有目标订单
                        {
                            if (_all_goods_num == ops[0].all_goods_num)//将所有子订单合成一个主订单。
                            {
                                //将所有子订单删除，更新订单产品表中的产品订单ID为主订单的ID
                                if (!SplitOrder(conn, trans, ops, 5))
                                {
                                    trans.Rollback();
                                    conn.Close();
                                    return false;
                                }
                            }
                            else
                            {
                                if (wait_del_ordre_ids.Count == 0)
                                {
                                    //将所有待分配产品ID移动到目标子订单中。
                                    if (!SplitOrder(conn, trans, ops, 4, target_order_id, null, wait_split_goods_ids))
                                    {
                                        trans.Rollback();
                                        conn.Close();
                                        return false;
                                    }
                                }
                                else
                                {
                                    //将待删除的子订单删除，将所有待分配产品ID移动到目标子订单中。

                                    if (!SplitOrder(conn, trans, ops, 3, target_order_id, wait_del_ordre_ids, wait_split_goods_ids))
                                    {
                                        trans.Rollback();
                                        conn.Close();
                                        return false;
                                    }
                                }
                            }
                        }

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        conn.Close();
                        return false;
                    }
                }
                conn.Close();
            }
            return true;
        }


        private int AddNewOrder(SqlConnection conn, SqlTransaction trans, int order_id)
        {
            DAL.orders odal = new DAL.orders(databaseprefix);
            Model.orders parentOrder = odal.GetModel(conn, trans, order_id);

            if (parentOrder != null)
            {
                Model.orders newOrder = new Model.orders();
                newOrder = parentOrder;
                newOrder.order_no = Utils.GetOrderNumber();
                newOrder.order_amount = 0;
               
                return odal.Add(conn, trans, newOrder);
            }
            else
            {
                return 0;
            }
        }

        private bool SplitOrder(SqlConnection conn, SqlTransaction trans,
            List<Model.order_product_split> mops, int type, int tag_order_id = 0,
            List<int> del_order_ids = null,
            List<int> wait_goods_ids = null,
            List<int> other_goods_ids = null)
        {
            int newOrderId = 0;
            string goods_ids = "";

            string order_no1 = string.Empty;
            string order_no2 = string.Empty;
            string order_no3 = string.Empty;

            switch (type)
            {
                case 1://创建两个新订单，将所有待分配产品ID移动到第一个子订单中，其余的移动到第二个子订单。
                    newOrderId = AddNewOrder(conn, trans, mops[0].order_id);

                    Model.orders o = new orders(databaseprefix).GetModel(conn, trans, mops[0].order_id);
                    order_no1 = o.order_no;

                    if (newOrderId == 0)
                    {
                        return false;
                    }
                    o = new orders(databaseprefix).GetModel(conn, trans, newOrderId);
                    order_no2 = o.order_no;

                    try
                    {
                        if (wait_goods_ids != null)
                        {
                            foreach (int v in wait_goods_ids)
                            {
                                goods_ids += v + ",";
                            }
                        }

                        if (goods_ids == "")
                        {
                            return false;
                        }
                        else
                        {
                            goods_ids = goods_ids.Substring(0, goods_ids.Length - 1);
                        }


                        //将所有待分配产品ID移动到第一个子订单中
                        DbHelperSQL.ExecuteSql(conn, trans, "update dt_order_goods set order_id=" + newOrderId + " where order_id=" + mops[0].order_id + " and goods_id in(" + goods_ids + ")");

                        string other_ids = "";

                        if (other_goods_ids != null)
                        {
                            foreach (int v in other_goods_ids)
                            {
                                other_ids += v + ",";
                            }
                        }

                        if (other_ids == "")
                        {
                            return false;
                        }
                        else
                        {
                            other_ids = other_ids.Substring(0, other_ids.Length - 1);
                        }


                        newOrderId = AddNewOrder(conn, trans, mops[0].order_id);
                        if (newOrderId == 0)
                        {
                            return false;
                        }
                        //其余的移动到第二个子订单
                        DbHelperSQL.ExecuteSql(conn, trans, "update dt_order_goods set order_id=" + newOrderId + " where order_id=" + mops[0].order_id + " and goods_id not in(" + goods_ids.Substring(0, goods_ids.Length - 1) + ")");

                        o = new orders(databaseprefix).GetModel(conn, trans, newOrderId);
                        order_no3 = o.order_no;


                        //记录日志
                        new logs(databaseprefix).Add(conn, trans, new Model.logs
                        {
                            log_type = 1,
                            operation_type = "分单",
                            operation_time = DateTime.Now,
                            operation_content = "将原订单：" + order_no1 + " 分为两个订单，订单1：" + order_no2 + "，包含商品：" + get_goods_titles(conn, trans, goods_ids) + "；订单2：" + order_no3 + ",包含商品：" + get_goods_titles(conn, trans, other_ids)
                        });

                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                case 2://创建一个新订单，将所有待分配产品ID都移动到新订单下面。
                    newOrderId = AddNewOrder(conn, trans, mops[0].order_id);
                    if (newOrderId == 0)
                    {
                        return false;
                    }

                    try
                    {
                        string orderIds = "";

                        foreach (Model.order_product_split ops in mops)
                        {
                            orderIds += ops.order_id + ",";
                        }

                        if (orderIds == "")
                        {
                            return false;
                        }
                        else
                        {
                            orderIds = orderIds.Substring(0, orderIds.Length - 1);
                        }

                        if (wait_goods_ids != null)
                        {
                            foreach (int v in wait_goods_ids)
                            {
                                goods_ids += v + ",";
                            }
                        }

                        if (goods_ids == "")
                        {
                            return false;
                        }
                        else
                        {
                            goods_ids = goods_ids.Substring(0, goods_ids.Length - 1);
                        }


                        //将所有待分配产品ID移动到新订单中
                        DbHelperSQL.ExecuteSql(conn, trans, "update dt_order_goods set order_id=" + newOrderId + " where order_id in(" + orderIds + ") and goods_id in(" + goods_ids + ")");


                        //记录日志
                        new logs(databaseprefix).Add(conn, trans, new Model.logs
                        {
                            log_type = 1,
                            operation_type = "分单",
                            operation_time = DateTime.Now,
                            operation_content = "将订单：" + get_order_titles(conn, trans, orderIds) + " 下的产品【" + get_goods_titles(conn, trans, goods_ids) + "】 移动到 新订单：" + get_order_titles(conn, trans, newOrderId.ToString()) + "下。"
                        });

                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                case 3://将待删除的子订单删除，将所有待分配产品ID移动到目标子订单中。

                    try
                    {
                        if (wait_goods_ids != null)
                        {
                            foreach (int v in wait_goods_ids)
                            {
                                goods_ids += v + ",";
                            }
                        }

                        if (goods_ids == "")
                        {
                            return false;
                        }
                        else
                        {
                            goods_ids = goods_ids.Substring(0, goods_ids.Length - 1);
                        }

                        string delIds = "";
                        foreach (int v in del_order_ids)
                        {
                            delIds += v + ",";
                        }

                        if (delIds == "")
                        {
                            return false;
                        }
                        else
                        {
                            delIds = delIds.Substring(0, delIds.Length - 1);
                        }

                        string orderIds = "";

                        foreach (Model.order_product_split ops in mops)
                        {
                            orderIds += ops.order_id + ",";
                        }

                        if (orderIds == "")
                        {
                            return false;
                        }
                        else
                        {
                            orderIds = orderIds.Substring(0, orderIds.Length - 1);
                        }

                        //将所有待分配产品ID移动到目标子订单中。                        
                        DbHelperSQL.ExecuteSql(conn, trans, "update dt_order_goods set order_id=" + tag_order_id + " where order_id in(" + orderIds + ") and goods_id in(" + goods_ids + ")");


                        //记录日志
                        new logs(databaseprefix).Add(conn, trans, new Model.logs
                        {
                            log_type = 1,
                            operation_type = "分单",
                            operation_time = DateTime.Now,
                            operation_content = "将订单：" + get_order_titles(conn, trans, orderIds) + " 下的产品【" + get_goods_titles(conn, trans, goods_ids) + "】 移动到 订单：" + get_order_titles(conn, trans, tag_order_id.ToString()) + "下,删除订单：" + get_order_titles(conn, trans, delIds) + " 。"
                        });


                        //将待删除的子订单删除
                        DbHelperSQL.ExecuteSql(conn, trans, "delete dt_orders where id in(" + delIds + ")");

                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                case 4://将所有待分配产品ID移动到目标子订单中。

                    try
                    {
                        if (wait_goods_ids != null)
                        {
                            foreach (int v in wait_goods_ids)
                            {
                                goods_ids += v + ",";
                            }
                        }

                        if (goods_ids == "")
                        {
                            return false;
                        }
                        else
                        {
                            goods_ids = goods_ids.Substring(0, goods_ids.Length - 1);
                        }

                        string orderIds = "";

                        foreach (Model.order_product_split ops in mops)
                        {
                            orderIds += ops.order_id + ",";
                        }

                        if (orderIds == "")
                        {
                            return false;
                        }
                        else
                        {
                            orderIds = orderIds.Substring(0, orderIds.Length - 1);
                        }

                        //将所有待分配产品ID移动到目标子订单中。                        
                        DbHelperSQL.ExecuteSql(conn, trans, "update dt_order_goods set order_id=" + tag_order_id + " where order_id in(" + orderIds + ")  and goods_id in(" + goods_ids + ")");


                        //记录日志
                        new logs(databaseprefix).Add(conn, trans, new Model.logs
                        {
                            log_type = 1,
                            operation_type = "分单",
                            operation_time = DateTime.Now,
                            operation_content = "将订单：" + get_order_titles(conn, trans, orderIds) + " 下的产品【" + get_goods_titles(conn, trans, goods_ids) + "】 移动到 订单：" + get_order_titles(conn, trans, tag_order_id.ToString()) + "下。"
                        });

                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                case 5://将所有子订单删除，更新订单产品表中的产品订单ID为主订单的ID

                    try
                    {
                        string orderIds = "";

                        foreach (Model.order_product_split ops in mops)
                        {
                            orderIds += ops.order_id + ",";
                        }

                        if (orderIds == "")
                        {
                            return false;
                        }
                        else
                        {
                            orderIds = orderIds.Substring(0, orderIds.Length - 1);
                        }

                        int pid = mops[0].parent_id;

                        //更新订单产品表中的产品订单ID为主订单的ID     
                        DbHelperSQL.ExecuteSql(conn, trans, "update dt_order_goods set order_id=" + pid + " where order_id in(" + orderIds.Substring(0, orderIds.Length - 1) + ")");

                        //将所有子订单删除
                        DbHelperSQL.ExecuteSql(conn, trans, "delete dt_orders where id in(" + orderIds + ")");

                        //记录日志
                        new logs(databaseprefix).Add(conn, trans, new Model.logs
                        {
                            log_type = 1,
                            operation_type = "分单",
                            operation_time = DateTime.Now,
                            operation_content = "删除订单：" + get_order_titles(conn, trans, orderIds) + ",并将它们下边的所有产品移动到 主订单：" + get_order_titles(conn, trans, pid.ToString()) + "下。"
                        });


                        return true;
                    }
                    catch
                    {
                        return false;
                    }

            }
            return false;
        }

        private string get_goods_titles(SqlConnection conn, SqlTransaction trans, string goods_ids)
        {
            object obj = DbHelperSQL.GetSingle(conn, trans, "select replace(replace((stuff((select g.goods_title title from dt_order_goods_logs g where g.goods_id in(" + goods_ids + ") for xml path('')),1,7,'')),'</title><title>','，'),'</title>','') goods_title");
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        private string get_order_titles(SqlConnection conn, SqlTransaction trans, string order_ids)
        {
            object obj = DbHelperSQL.GetSingle(conn, trans, "select replace(replace((stuff((select o.order_no title from dt_orders o where o.id in(" + order_ids + ") for xml path('')),1,7,'')),'</title><title>','，'),'</title>','') order_no");
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        #endregion
    }
}

